﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.UserSetting
{
    public partial class MessageList : BasePage
    {
        private string fromurl = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            fromurl = Request.QueryString["from"] + "";

            if (!Page.IsPostBack)
            {
                LoadContent(false);
            }
        }

        #region 加载内容
        private void LoadContent(bool mark)
        {
            if (fromurl != "")
            {
                litTitle.Text = "新消息列表";
            }
            else
            {
                litTitle.Text = "消息列表";
            }
            //设置控件属性，从BasePage中得到
            PageConfig(MessageUPager);

            //List<Member> list = new List<Member();
            MessageHelper mhelper = new MessageHelper();
            Criteria c = new Criteria();
            Users uinfo = UserBase.GetCurrentUser();
            c.Add(CriteriaType.Equals, "ToUsersID", uinfo.ID);
            c.Add(CriteriaType.Equals, "Flag", 0);
            if (fromurl != "")
            {
                c.Add(CriteriaType.Equals, "Status", 0);
            }

            MessageUPager.ItemCount = mhelper.QueryMessageCountBySearch(c);

            // 以下的值从BasePage中得到
            MessageUPager.UrlFormat = UrlFormat;
            MessageUPager.PrefixText = GetPreFixText(MessageUPager);

            if (mark)
            {
                PagerInit(MessageUPager);
            }

            string[] fields = new string[] { "ID", "FromUsersID", "FromNickName", "ToUsersID", "MessageContent", "Flag", "Status", "CreateDate" };

            DataTable dt = new DataTable();
            dt = mhelper.QueryMessageBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, MessageUPager.Begin - 1, MessageUPager.Count, fields);

            rptMessageList.DataSource = dt;
            rptMessageList.DataBind();

            //将所有信息改为已读
            Message minfo = new Message();
            minfo.Status = 1;
            string[] editfields = new string[] { "Status" };
            Criteria c1 = new Criteria();
            c1.Add(CriteriaType.Equals, "ToUsersID", uinfo.ID);

            mhelper.EditMessage(minfo, editfields, c1);//批量修改
        }
        #endregion

        #region 列表事件
        protected void rptMessageList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HiddenField hidFromUserID = (HiddenField)e.Item.FindControl("hidFromUserID");

            string id = hidID.Value;
            string fromuserid = hidFromUserID.Value;

            if (e.CommandName == "delete")//删除
            {
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                minfo = mhelper.GetMessage(id);
                minfo.Flag = 9;
                mhelper.EditMessage(minfo);
                LoadContent(false);
            }
            else if (e.CommandName == "reply")//回复 
            {
                Response.Redirect("MessageReply.aspx?touserid="+fromuserid);
            }
        }
        #endregion

        #region 删除选择
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Users uinfo = UserBase.GetCurrentUser();
            MessageHelper mhelper = new MessageHelper();
            mhelper.DeleteAllByUserID(uinfo.ID);
            Tools.AlertAndRedirect("消息清空成功", "MessageList.aspx");
            return;
        }
        #endregion
    }
}